﻿/*!
 * Onejs Core Library Version 1.0.0
 * Date: 2012-05-16 17:45:11
 * Copyright © OneStudio, Inc. All Rights Reserved. 
 */

(function( o, w, $ ){

var document = w.document,
    expando = 'ONE' + (+new Date),

// 动画核心引擎函数
animate =
{
    fx: function( ontween, onend, duration )
	{
		var pre, runTime,
		    startTime = +new Date,
		
		timer = setInterval(function()
		{
			runTime = +new Date - startTime;
			pre = runTime / duration;
			if( pre >= 1 )
			{
				clearInterval(timer);
				onend(pre);
			}
			else
				ontween(pre);
		}, 13 );
	}
};

w[o] = 
{
    tool:
	{
	    clone: function( object )
		{
		    function f(){};
			f.prototype = object;
			return new f;
		},
		
		extend: function( object, target )
		{
		    for( var name in target )
			{
			    if( object[name] === undefined ) object[name] = target[name];
			}
		},
		
		// 取目标元素八个点的坐标
		offset: function( elem, position, offset )
		{
		    var cr = $(elem).offset(), X, Y,
			    eX = cr.left,
				eY = cr.top,
				eW = elem.offsetWidth,
				eH = elem.offsetHeight,
				xc = position[0] || 'left',
				yc = position[1] || 'bottom';
			
			if( xc === 'left' )
			    X = eX;
			else if( xc === 'center' )
			    X = eX + eW/2;
			else if( xc === 'right' )
			    X = eX + eW;
			
			if( yc === 'top' )
			    Y = eY;
			else if( yc === 'center' )
			    Y = eY + eH/2;
			else if( yc === 'bottom' )
			    Y = eY + eH;
			
			X = X + offset[0];
			Y = Y + offset[1];
			
			return { x:X, y:Y };
		}
	},
	
	anim:
	{
	    base: function( elem, css, duration, callback )
		{
			function ontween(pre)
			{
				var obj, val, form, to, name, unit,
					i = 0, len = css.length;
					
				for( ; i < len; i++ )
				{
					obj = css[i];
					from = obj[0];
					to = obj[1];
					name = obj[2];
					unit = obj[3];
					
					val = from + (to - from) * pre;
					elem.style[name] = val + unit;
				}
			};
			
			function onend(pre)
			{
				ontween(pre);
				callback && callback.call(elem);
			};
			
			animate.fx( ontween, onend, duration || this.duration );
		},
		
		duration: 800
	},
	
	plugin: {}
};

})( 'onejs', window, jQuery );